using Abp.AspNetCore.Mvc.Authorization;
using Abp.Extensions;
using Abp.UI;
using Microsoft.AspNetCore.Mvc;
using Mt.Site.Application;
using Mt.Site.Application.FileUpload.Dto;
using Mt.Site.Application.FileUpload.Dto.FileUpload;
using Mt.Site.Application.FileUpload.Impl;
using Mt.Site.Core.Base.Utils.FileIO.Upload;
using Mt.Site.Core.Storage;
using Mt.Site.Core.Storage.File_;
using Mt.Site.Core.Synchronization;
using Mt.Site.Web.Controllers;
using System.Threading.Tasks;
using static Mt.Site.Core.Base.Utils.FileIO.Upload.UpfileTemplate;

namespace Mt.Site.Web.Areas.B.Controllers
{
    [AbpMvcAuthorize]
    [Area("B")]
    public class FileUploadController : MtSiteControllerBase
    {
        private readonly IFileUpload _fileUpload;

        private readonly IFileLibraryManager _fileLibraryManager;
        private readonly IFileObjectManager _fileObjectManager;

        private readonly FileUploadHelper _fileUploadHelper;

        private readonly IUploadStrategy _uploadStrategy;

        public FileUploadController(
            IFileObjectManager fileObjectManager,
            IFileUpload fileUpload,
            IFileLibraryManager fileLibraryManager,
            FileUploadHelper fileUploadHelper,
            IUploadStrategy uploadStrategy)
        {
            _fileObjectManager = fileObjectManager;
            _fileUpload = fileUpload;

            _fileLibraryManager = fileLibraryManager;

            _fileUploadHelper = fileUploadHelper;
            _uploadStrategy = uploadStrategy;
        }


        #region 文件上传

        /// <summary>
        /// 上传文件（批量）
        /// </summary>
        /// <returns></returns>
        public async Task<FileInfoDto> UploadFileAsync(UploadFileInput input)
        {
            Para para = await _fileUploadHelper.GetUpFileConfig(input.CustomKey, input.Alias);

            var upfileInfo = para.UpFileInfo;
            var upfileResult = await _fileUpload.UploadFile(upfileInfo, null, HttpContext, _uploadStrategy);

            if (upfileResult.IsOk)
            {
                var fileInfoDto = await _fileUploadHelper.CreateFileInfo(
                    _fileObjectManager, input.ContentId, input.Alias, input.IsTemporary, upfileResult.UploadFileInfo, 
                    upfileResult.FileName, upfileResult.ExName, upfileResult.SourceName);

                await _fileUploadHelper.OnMultiUploaded(fileInfoDto.FileUrl, fileInfoDto.Id);

                await _fileUploadHelper.RemoveStaticPageAndSyncFileObject(input.ContentId, fileInfoDto.Id, OptionType.Create, AbpSession);

                return fileInfoDto;
            }
            else
            {
                throw new UserFriendlyException(upfileResult.ExMsg);
            }
        }

        /// <summary>
        /// 单个文件上传
        /// </summary>
        /// <returns></returns>
        public async Task<FileInfoDto> SingleUploadAsync(SingleUploadFileInput input)
        {
            Para para = await _fileUploadHelper.GetUpFileConfig(input.CustomKey, input.Alias);

            string alias = para.Alias;
            if (!input.Alias.IsNullOrWhiteSpace())
            {
                alias = input.Alias;
            }

            var upfileResult = await _fileUpload.UploadFile(para.UpFileInfo, null, HttpContext, _uploadStrategy);

            if (upfileResult.IsOk)
            {
                var fileInfoDto = await _fileUploadHelper.CreateFileInfo(_fileObjectManager, input.ContentId, alias, input.IsTemporary, upfileResult.UploadFileInfo, upfileResult.FileName, upfileResult.ExName, upfileResult.SourceName);

                await _fileUploadHelper.OnSingleUploaded(fileInfoDto.FileUrl, fileInfoDto.Id);

                await _fileUploadHelper.RemoveStaticPageAndSyncFileObject(input.ContentId, fileInfoDto.Id, OptionType.Create, AbpSession);

                return fileInfoDto;
            }
            else
            {
                throw new UserFriendlyException(upfileResult.ExMsg);
            }
        }

        /// <summary>
        /// 上传控件二
        /// </summary>
        /// <returns></returns>
        public async Task<UpdateUploadFileOut> FileUploadDealTwoAsync(FileUploadDealTwoInput input)
        {
            var para = await _fileUploadHelper.GetUpFileConfig(input.CustomKey, input.Alias);

            var upfileResult = await _fileUpload.UploadFile(para.UpFileInfo, null, HttpContext, _uploadStrategy);
            if (upfileResult.IsOk)
            {
                if (upfileResult.SourceName != FileScoureName.FromLibrary)
                {
                    await _fileLibraryManager.AddToFileLibraryAutoAsync(upfileResult.UploadFileInfo, upfileResult.ExName);
                }

                return new UpdateUploadFileOut
                {
                    ImgUrl = upfileResult.UploadFileInfo.FileUrl
                };
            }
            else
            {
                throw new UserFriendlyException(upfileResult.ExMsg);
            }
        }

        // FileObject编辑
        public async Task<UpdateUploadFileOut> FileUploadForEditAsync(FileUploadForEditInput input)
        {
            var para = await _fileUploadHelper.GetUpFileConfig(input.CustomKey, input.Alias);

            UpFileInfo upFileInfo = input.IsPreviewImg ? para.ThumbnailUpFileInfo : para.UpFileInfo;

            var upfileResult = await _fileUpload.UploadFile(upFileInfo, null, HttpContext, _uploadStrategy);
            if (upfileResult.IsOk)
            {
                await _fileLibraryManager.TryAddToFileTemporaryAutoAsync(upfileResult);

                return new UpdateUploadFileOut
                {
                    ImgUrl = upfileResult.UploadFileInfo.FileUrl
                };
            }
            else
            {
                throw new UserFriendlyException(upfileResult.ExMsg);
            }
        }

        #endregion

        #region 水印图上传

        public async Task<ActionResult> UploadWaterMarkImageAsync()
        {
            var saveName = (AbpSession.TenantId ?? 0) + ".png";

            var para = await _fileUploadHelper.GetUpFileConfig("waterMark", null);     // 获取模板

            var upfileResult = await _fileUpload.UploadFile(para.UpFileInfo, saveName, null, HttpContext);
            if (upfileResult.IsOk)
            {
                return Json(upfileResult.UploadFileInfo.FileUrl);
            }
            else
            {
                throw new UserFriendlyException(upfileResult.ExMsg);
            }
        }



        #endregion
    }
}
